package filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录检查过滤器
 * 
 * @author 崔诗新
 *
 */
@WebFilter("/admin/*")
public class Filter1_AdminLoginCheckFilter implements Filter {

	public Filter1_AdminLoginCheckFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		HttpServletResponse resp = (HttpServletResponse) response;
		String path = req.getServletPath();
		
		if (!path.substring(1).equals("admin/login.jsp") && !path.substring(1).equals("LoginServlet")
				&& !path.contains("admin/css")&& !path.contains("admin/img")
				&& !path.contains("admin/js")&& !path.substring(1, 4).equals("css") 
				&& !path.substring(1, 6).equals("image")&& !path.substring(1, 3).equals("js") ) {
			Boolean isLogin = (Boolean) session.getAttribute("isLogin");
			if (isLogin == null || !isLogin) {
				resp.sendRedirect(req.getContextPath() + "/admin/login.jsp");
				return;
			}
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
